var pager;
var usersCount = 0;
var recordsToBeShown;

function Pager(tableName, itemsPerPage, pagerName, positionId) {
    this.tableName = tableName;
    this.itemsPerPage = itemsPerPage;
	this.pagerName = pagerName;
	this.positionId = positionId;
    this.currentPage = 1;
    this.pages = 0;
    this.inited = false;
    
    this.showRecords = function() {    
		var userTable = $('#result-table');
        var tbody = userTable.find('tbody');
        tbody.empty();
        $.each(recordsToBeShown.userlist, function (index, element)
		{
			var statusId = "st" + element.id.toString();
			var checkboxId = "ch" + element.id.toString();
			rowstring = '<tr name="rat" id='+element.id.toString()+'>';
			rowstring += '<td><input type="checkbox" name="checkboxes" id='+checkboxId+' onclick="javascript:updateMainCheckBox()" ></td>';
			rowstring += '<td>' + element.login + '</td>';
			rowstring += '<td>' + element.name + '</td>';
			rowstring += '<td>' + element.surname + '</td>';
			var status = element.groupId.value == VIPId ? 'VIP' : 'Basic';
			rowstring += '<td id='+statusId+'>' + status + '</td>';
			rowstring += '</tr>';
			userTable.append(rowstring);
			document.getElementById(element.id.toString()).style.background = (index % 2 == 1 ? 'white' : '#C5FEFE');
		});
    }
    
    this.showPage = function(pageNumber) {
    	if (! this.inited) {
    		alert("not inited");
    		return;
    	}
		
		getUsersCount();
        this.pages = Math.ceil(usersCount / itemsPerPage);
        this.currentPage = pageNumber;
		this.showPageNav();
        
        getNeededUsers(pageNumber,itemsPerPage);
        this.showRecords();
    }   
    
    this.prev = function() {
        if (this.currentPage > 1)
            this.showPage(this.currentPage - 1);
    }
    
    this.next = function() {
        if (this.currentPage < this.pages) {
            this.showPage(this.currentPage + 1);
        }
    }                        
    
    this.init = function() {
        this.inited = true;
    }

    this.showPageNav = function() {
    	if (! this.inited) {
    		alert("not inited");
    		return;
    	}
		var curPage = this.currentPage, pageCount = this.pages;
    	var element = document.getElementById(positionId);
		var pagerHtml = "";
    	//Left arrow
		if (curPage > 1)
    		pagerHtml += '<span onclick="' + pagerName + '.prev();" class="pg-pointer">&larr;</span>&nbsp;&nbsp;';
		//First page
		if (this.currentPage > 2)
			pagerHtml += '<span id="pg' + 1 + '" class='+category(1,curPage)+' onclick="' + pagerName + '.showPage(' + 1 + ');">' + 1 + '</span>&nbsp;&nbsp;';
		//Three dots
		if (this.currentPage > 3)
			pagerHtml += "...&nbsp;&nbsp;";
		//Three middle pages
        for (var page = Math.max(1,curPage-1); page <= Math.min(pageCount,curPage+1); page++) 
            pagerHtml += '<span id="pg' + page + '" class='+category(page,curPage)+' onclick="' + pagerName + '.showPage(' + page + ');">' + page + '</span>&nbsp;&nbsp;';
		//Three dots
		if (this.currentPage < pageCount - 2)
			pagerHtml += "...&nbsp;&nbsp;";
		//Last page
		if (this.currentPage < pageCount - 1)
			pagerHtml += '<span id="pg' + pageCount + '" class='+category(pageCount,curPage)+' onclick="' + pagerName + '.showPage(' + pageCount + ');">' + pageCount + '</span>&nbsp;&nbsp;';
		//Right arrow
		if (curPage < pageCount)
        	pagerHtml += '<span onclick="'+pagerName+'.next();" class="pg-pointer">&rarr;</span>';            
        
        element.innerHTML = pagerHtml;
    }
	
	function category(number,curPage) {
		return (number == curPage ? "pg-selected" : "pg-normal");
	}
}

